﻿using Dispatcher.Tasks.Models;
using Microsoft.AspNetCore.Mvc;
using MiniFox;
using MiniFox.Infrastructure;
using MiniFox.Platform.Applications.Web;
using MiniFox.Web;
using MiniFox.Web.Infrastructure;
using Newtonsoft.Json.Linq;

namespace Dispatcher.Tasks
{
    public class QueueController : AuthorizedController<QueueService>
    {
        [HttpPost]
        public Response Push([FromRoute]string name, [FromBody]JObject request)
        {
            var contextId = SequenceGUID.NewGUID();
            this.Component.Push(contextId, request, this.Request.GetQueryString());

            return response;
        }
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="contextId"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public async Task<GeneralResponse<FileData[]>> Attach([FromUri]string name, [FromUri]string contextId)
        //{
        //    GeneralResponse<FileData[]> response = new GeneralResponse<FileData[]>(contextId);

        //    ApiAuthClient apiAuthClient = this.Request.GetApiAuthClient();
        //    var accessToken = apiAuthClient.AccessToken;
        //    var fileDatas = await this.Request.HttpFileBatchLoadAsync();

        //    var service = this.Container.GetComponentByName<QueueService>(name, name);
        //    service.SetAttachments(accessToken, contextId, fileDatas);
        //    response.Data = fileDatas.ToArray();

        //    return response;
        //}

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="contextId"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public async Task<GeneralResponse<FileData[]>> AttachAsync([FromUri] string name, [FromUri] string contextId)
        //{
        //    GeneralResponse<FileData[]> response = new GeneralResponse<FileData[]>(contextId);

        //    ApiAuthClient apiAuthClient = this.Request.GetApiAuthClient();
        //    var accessToken = apiAuthClient.AccessToken;
        //    var fileDatas = await this.Request.HttpFileBatchLoadAsync();

        //    var service = this.Container.GetComponentByName<QueueService>(name, name);
        //    await service.SetAttachmentsAsync(accessToken, contextId, fileDatas);
        //    response.Data = fileDatas.ToArray();

        //    return response;
        //}

        [HttpGet]
        public GeneralResponse<string[]> Groups([FromUri] string contextId)
        {
            var response = new GeneralResponse<string[]>(contextId);
            var items = QueueFactory.LoadGroup();
            if (items.Length > 0)
                response.Data = items;
            return response;
        }

        [HttpGet]
        public GeneralResponse<QueueItem[]> Items([FromUri] string contextId)
        {
            var response = new GeneralResponse<QueueItem[]>(contextId);
            var items = QueueFactory.LoadQueue();
            if (items.Length > 0)
                response.Data = items;
            return response;
        }

        [HttpPost]
        public GeneralResponse<QueueItem[]> Items(GeneralRequest<string> request)
        {
            var response = new GeneralResponse<QueueItem[]>(request);
            string name = request.Data;
            var service = this.Container.GetComponentByName<QueueService>(name, name);
            var items = service.Queue.ToArray();
            if (items.Length > 0)
                response.Data = items;
            return response;
        }

    }
}
